package com.mall.web.controller.order.app;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.order.model.dto.AppCreateOrderDto;
import com.mall.order.model.dto.OrderDto;
import com.mall.order.model.vo.OrderVo;
import com.mall.order.service.IAppOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/order/order")
@Api(tags = "用户端小程序=》订单表")
public class AppOrderController extends BaseController {

    @Autowired
    private IAppOrderService orderService;


    @GetMapping("/page")
    @ApiOperation("分页")
    public TableDataInfo page(OrderDto dto) {
        startPage();
        List<OrderVo> list = orderService.page(dto);
        if (null != list && list.size() > 0) {
            list.forEach(listItem-> {
                if (null != listItem) {
                    listItem.setTradeNo("");
                }
            });
        }
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    @ApiOperation("详情")
    public AjaxResult detail(@PathVariable("id") Long id) {
        OrderVo orderVo = orderService.detail(id);
        if (null != orderVo) {
            orderVo.setTradeNo("");
        }
        return success(orderVo);
    }

    @Log(title = "创建订单", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    @ApiOperation("创建订单")
    public AjaxResult create(@RequestBody AppCreateOrderDto dto) {
        return success(orderService.create(dto));
    }

    @Log(title = "创建预支付订单", businessType = BusinessType.INSERT)
    @PostMapping("/createPrePayOrder")
    @ApiOperation("创建订单")
    public AjaxResult createPrePayOrder(@RequestParam String openId, @RequestParam Long orderId) {
        return success(orderService.createPrePayOrder(openId, orderId));
    }

    @Log(title = "微信支付结果通知", businessType = BusinessType.UPDATE)
    @PostMapping("/wechatPay")
    @ApiOperation("微信支付结果通知")
    public AjaxResult wechatPay(HttpServletRequest request) {
        this.orderService.wechatPay(request);
        return success();
    }

    @Log(title = "取消订单", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public AjaxResult cancel(@PathVariable("id") Long id) {
        this.orderService.cancel(id);
        return success();
    }

    @Log(title = "确认收货", businessType = BusinessType.UPDATE)
    @PutMapping("/receipt/{id}")
    @ApiOperation("确认收货")
    public AjaxResult receipt(@PathVariable("id") Long id) {
        orderService.receipt(id);
        return success();
    }
}
